<?php

/*============================================
Classe SysMenuItems
============================================*/

class SysMenuItems extends ModelObject{

	protected static $cached_object_list= array();
	protected $MENU_ITEM_ID;
	protected $MENU_ITEM_LABEL;
	protected $MENU_ITEM_POSITION;
	protected $MENU_MNEMO;
	protected $MENU_FATHER_POSITION;
	protected $MENU_ITEM_PRIVILEGE;
	protected $MENU_ITEM_COMMAND_TYPE;
	protected $MENU_ITEM_SEPARATOR;
	protected $MENU_ITEM_COMMAND;
	protected $MENU_ITEM_CMD_WINDOW;

	protected function __construct( $MENU_ITEM_ID,$MENU_ITEM_LABEL,$MENU_ITEM_POSITION,$MENU_MNEMO,$MENU_FATHER_POSITION,$MENU_ITEM_PRIVILEGE,$MENU_ITEM_COMMAND_TYPE,$MENU_ITEM_SEPARATOR,$MENU_ITEM_COMMAND,$MENU_ITEM_CMD_WINDOW){
		try{
			
			$this->MENU_ITEM_ID = $MENU_ITEM_ID;
			$this->MENU_ITEM_LABEL = $MENU_ITEM_LABEL;
			$this->MENU_ITEM_POSITION = $MENU_ITEM_POSITION;
			$this->MENU_MNEMO = $MENU_MNEMO;
			$this->MENU_FATHER_POSITION = $MENU_FATHER_POSITION;
			$this->MENU_ITEM_PRIVILEGE = $MENU_ITEM_PRIVILEGE;
			$this->MENU_ITEM_COMMAND_TYPE = $MENU_ITEM_COMMAND_TYPE;
			$this->MENU_ITEM_SEPARATOR = $MENU_ITEM_SEPARATOR;
			$this->MENU_ITEM_COMMAND = $MENU_ITEM_COMMAND;
			$this->MENU_ITEM_CMD_WINDOW = $MENU_ITEM_CMD_WINDOW;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getMENUITEMID(){
		return $this->MENU_ITEM_ID;
	}

	public function getMENUITEMLABEL(){
		return $this->MENU_ITEM_LABEL;
	}

	public function getMENUITEMPOSITION(){
		return $this->MENU_ITEM_POSITION;
	}

	public function getMENUMNEMO(){
		return $this->MENU_MNEMO;
	}

	public function getMENUFATHERPOSITION(){
		return $this->MENU_FATHER_POSITION;
	}

	public function getMENUITEMPRIVILEGE(){
		return $this->MENU_ITEM_PRIVILEGE;
	}

	public function getMENUITEMCOMMANDTYPE(){
		return $this->MENU_ITEM_COMMAND_TYPE;
	}

	public function getMENUITEMSEPARATOR(){
		return $this->MENU_ITEM_SEPARATOR;
	}

	public function getMENUITEMCOMMAND(){
		return $this->MENU_ITEM_COMMAND;
	}

	public function getMENUITEMCMDWINDOW(){
		return $this->MENU_ITEM_CMD_WINDOW;
	}

	public function setMENUITEMLABEL( $MENU_ITEM_LABEL){
		try{
			$temp = $this->MENU_ITEM_LABEL;
			if($this->MENU_ITEM_LABEL !== $MENU_ITEM_LABEL){
				$this->MENU_ITEM_LABEL = $MENU_ITEM_LABEL;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_ITEM_LABEL = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUITEMLABEL() a échouée",0,$e);
		}
	}

	public function setMENUITEMPOSITION( $MENU_ITEM_POSITION){
		try{
			$temp = $this->MENU_ITEM_POSITION;
			if($this->MENU_ITEM_POSITION !== $MENU_ITEM_POSITION){
				$this->MENU_ITEM_POSITION = $MENU_ITEM_POSITION;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_ITEM_POSITION = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUITEMPOSITION() a échouée",0,$e);
		}
	}

	public function setMENUMNEMO( $MENU_MNEMO){
		try{
			$temp = $this->MENU_MNEMO;
			if($this->MENU_MNEMO !== $MENU_MNEMO){
				$this->MENU_MNEMO = $MENU_MNEMO;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_MNEMO = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUMNEMO() a échouée",0,$e);
		}
	}

	public function setMENUFATHERPOSITION( $MENU_FATHER_POSITION){
		try{
			$temp = $this->MENU_FATHER_POSITION;
			if($this->MENU_FATHER_POSITION !== $MENU_FATHER_POSITION){
				$this->MENU_FATHER_POSITION = $MENU_FATHER_POSITION;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_FATHER_POSITION = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUFATHERPOSITION() a échouée",0,$e);
		}
	}

	public function setMENUITEMPRIVILEGE( $MENU_ITEM_PRIVILEGE){
		try{
			$temp = $this->MENU_ITEM_PRIVILEGE;
			if($this->MENU_ITEM_PRIVILEGE !== $MENU_ITEM_PRIVILEGE){
				$this->MENU_ITEM_PRIVILEGE = $MENU_ITEM_PRIVILEGE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_ITEM_PRIVILEGE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUITEMPRIVILEGE() a échouée",0,$e);
		}
	}

	public function setMENUITEMCOMMANDTYPE( $MENU_ITEM_COMMAND_TYPE){
		try{
			$temp = $this->MENU_ITEM_COMMAND_TYPE;
			if($this->MENU_ITEM_COMMAND_TYPE !== $MENU_ITEM_COMMAND_TYPE){
				$this->MENU_ITEM_COMMAND_TYPE = $MENU_ITEM_COMMAND_TYPE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_ITEM_COMMAND_TYPE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUITEMCOMMANDTYPE() a échouée",0,$e);
		}
	}

	public function setMENUITEMSEPARATOR( $MENU_ITEM_SEPARATOR){
		try{
			$temp = $this->MENU_ITEM_SEPARATOR;
			if($this->MENU_ITEM_SEPARATOR !== $MENU_ITEM_SEPARATOR){
				$this->MENU_ITEM_SEPARATOR = $MENU_ITEM_SEPARATOR;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_ITEM_SEPARATOR = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUITEMSEPARATOR() a échouée",0,$e);
		}
	}

	public function setMENUITEMCOMMAND( $MENU_ITEM_COMMAND){
		try{
			$temp = $this->MENU_ITEM_COMMAND;
			if($this->MENU_ITEM_COMMAND !== $MENU_ITEM_COMMAND){
				$this->MENU_ITEM_COMMAND = $MENU_ITEM_COMMAND;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_ITEM_COMMAND = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUITEMCOMMAND() a échouée",0,$e);
		}
	}

	public function setMENUITEMCMDWINDOW( $MENU_ITEM_CMD_WINDOW){
		try{
			$temp = $this->MENU_ITEM_CMD_WINDOW;
			if($this->MENU_ITEM_CMD_WINDOW !== $MENU_ITEM_CMD_WINDOW){
				$this->MENU_ITEM_CMD_WINDOW = $MENU_ITEM_CMD_WINDOW;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MENU_ITEM_CMD_WINDOW = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMENUITEMCMDWINDOW() a échouée",0,$e);
		}
	}

	public static function create( $MENU_ITEM_ID,$MENU_ITEM_LABEL,$MENU_ITEM_POSITION,$MENU_MNEMO,$MENU_FATHER_POSITION,$MENU_ITEM_PRIVILEGE,$MENU_ITEM_COMMAND_TYPE,$MENU_ITEM_SEPARATOR,$MENU_ITEM_COMMAND,$MENU_ITEM_CMD_WINDOW){
		try{
			$sysMenuItems = new SysMenuItems($MENU_ITEM_ID, $MENU_ITEM_LABEL, $MENU_ITEM_POSITION, $MENU_MNEMO, $MENU_FATHER_POSITION, $MENU_ITEM_PRIVILEGE, $MENU_ITEM_COMMAND_TYPE, $MENU_ITEM_SEPARATOR, $MENU_ITEM_COMMAND, $MENU_ITEM_CMD_WINDOW);
			$sysMenuItems->insert();
			SysMenuItems::cacheObject($sysMenuItems);
			return $sysMenuItems;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type SysMenuItems a échouée",0,$e);
		}
	}

	public static function get( $MENU_ITEM_ID){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($MENU_ITEM_ID) || $MENU_ITEM_ID === "") {
				return null;
			}
			if(SysMenuItems::existsInCache($MENU_ITEM_ID)) {
				return SysMenuItems::getFromCache($MENU_ITEM_ID);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `sys_menu_items` WHERE `MENU_ITEM_ID` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$MENU_ITEM_ID));
			if ($row = $stmt->fetch()){
				$sysMenuItems = new SysMenuItems( $row['MENU_ITEM_ID'],$row['MENU_ITEM_LABEL'],$row['MENU_ITEM_POSITION'],$row['MENU_MNEMO'],$row['MENU_FATHER_POSITION'],$row['MENU_ITEM_PRIVILEGE'],$row['MENU_ITEM_COMMAND_TYPE'],$row['MENU_ITEM_SEPARATOR'],$row['MENU_ITEM_COMMAND'],$row['MENU_ITEM_CMD_WINDOW']);
				$sysMenuItems->id =  $row['MENU_ITEM_ID'];
				$dbh->commit();
				SysMenuItems::cacheObject($sysMenuItems);
				return $sysMenuItems;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $MENU_ITEM_ID . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `sys_menu_items` SET `MENU_ITEM_LABEL` = :MENU_ITEM_LABEL, `MENU_ITEM_POSITION` = :MENU_ITEM_POSITION, `MENU_MNEMO` = :MENU_MNEMO, `MENU_FATHER_POSITION` = :MENU_FATHER_POSITION, `MENU_ITEM_PRIVILEGE` = :MENU_ITEM_PRIVILEGE, `MENU_ITEM_COMMAND_TYPE` = :MENU_ITEM_COMMAND_TYPE, `MENU_ITEM_SEPARATOR` = :MENU_ITEM_SEPARATOR, `MENU_ITEM_COMMAND` = :MENU_ITEM_COMMAND, `MENU_ITEM_CMD_WINDOW` = :MENU_ITEM_CMD_WINDOW WHERE `MENU_ITEM_ID` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':MENU_ITEM_LABEL'=>$this->MENU_ITEM_LABEL, ':MENU_ITEM_POSITION'=>$this->MENU_ITEM_POSITION, ':MENU_MNEMO'=>$this->MENU_MNEMO, ':MENU_FATHER_POSITION'=>$this->MENU_FATHER_POSITION, ':MENU_ITEM_PRIVILEGE'=>$this->MENU_ITEM_PRIVILEGE, ':MENU_ITEM_COMMAND_TYPE'=>$this->MENU_ITEM_COMMAND_TYPE, ':MENU_ITEM_SEPARATOR'=>$this->MENU_ITEM_SEPARATOR, ':MENU_ITEM_COMMAND'=>$this->MENU_ITEM_COMMAND, ':MENU_ITEM_CMD_WINDOW'=>$this->MENU_ITEM_CMD_WINDOW, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `sys_menu_items` WHERE `MENU_ITEM_ID` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			SysMenuItems::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `sys_menu_items` ( `MENU_ITEM_LABEL`, `MENU_ITEM_POSITION`, `MENU_MNEMO`, `MENU_FATHER_POSITION`, `MENU_ITEM_PRIVILEGE`, `MENU_ITEM_COMMAND_TYPE`, `MENU_ITEM_SEPARATOR`, `MENU_ITEM_COMMAND`, `MENU_ITEM_CMD_WINDOW`) VALUES (:MENU_ITEM_LABEL, :MENU_ITEM_POSITION, :MENU_MNEMO, :MENU_FATHER_POSITION, :MENU_ITEM_PRIVILEGE, :MENU_ITEM_COMMAND_TYPE, :MENU_ITEM_SEPARATOR, :MENU_ITEM_COMMAND, :MENU_ITEM_CMD_WINDOW)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':MENU_ITEM_LABEL'=>$this->MENU_ITEM_LABEL, ':MENU_ITEM_POSITION'=>$this->MENU_ITEM_POSITION, ':MENU_MNEMO'=>$this->MENU_MNEMO, ':MENU_FATHER_POSITION'=>$this->MENU_FATHER_POSITION, ':MENU_ITEM_PRIVILEGE'=>$this->MENU_ITEM_PRIVILEGE, ':MENU_ITEM_COMMAND_TYPE'=>$this->MENU_ITEM_COMMAND_TYPE, ':MENU_ITEM_SEPARATOR'=>$this->MENU_ITEM_SEPARATOR, ':MENU_ITEM_COMMAND'=>$this->MENU_ITEM_COMMAND, ':MENU_ITEM_CMD_WINDOW'=>$this->MENU_ITEM_CMD_WINDOW));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `sys_menu_items`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(SysMenuItems::existsInCache($row['MENU_ITEM_ID'])){
					$obj = SysMenuItems::getFromCache($row['MENU_ITEM_ID']);
				}
				else{
					$obj = new SysMenuItems( $row['MENU_ITEM_ID'],$row['MENU_ITEM_LABEL'],$row['MENU_ITEM_POSITION'],$row['MENU_MNEMO'],$row['MENU_FATHER_POSITION'],$row['MENU_ITEM_PRIVILEGE'],$row['MENU_ITEM_COMMAND_TYPE'],$row['MENU_ITEM_SEPARATOR'],$row['MENU_ITEM_COMMAND'],$row['MENU_ITEM_CMD_WINDOW']);
				}
				if(!is_null($obj)){
					$obj->id = $row['MENU_ITEM_ID'];
					SysMenuItems::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type SysMenuItems a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `sys_menu_items`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type SysMenuItems a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `sys_menu_items`';
			$sql_select = 'SELECT MENU_ITEM_ID FROM `sys_menu_items`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = SysMenuItems::getObjectFromCache($row['MENU_ITEM_ID']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						SysMenuItems::removeObjectFromCache($row['MENU_ITEM_ID']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type SysMenuItems a échouée",0,$e);
		}
	}

}